package com.demo.test.controller;

import java.util.List;

import com.demo.test.interceptor.UserInterceptor;
import com.demo.test.model.User;
import com.demo.test.service.UserService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

@Before(UserInterceptor.class)
public class UserController extends Controller {
	private UserService userService = new UserService();
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void index(){
		render("login.jsp");
	}
	//@Before(UserValidator.class)
	//@Before(SessionInViewInterceptor.class)
	public void login(){
		String username = this.getPara("username");
		String password = this.getPara("password");
		System.out.println("username:"+username+"||password:"+password);
		this.setAttr("name", "success");
		this.setSessionAttr("session", "myName");
		System.out.println(this.getSessionAttr("session"));
		List<User> users = this.userService.load();
		
		this.setAttr("users", users);
		render("success.jsp");
	}
	//@Before(SessionInViewInterceptor.class)
	public void addInput(){
		System.out.println("addInput");
		render("addInput.jsp");
	}
	public void add(){
		//this.getModel(User.class).save();
		User user = this.getModel(User.class);
		this.userService.save(user);
		redirect("/user");
	}
	public void delete(){
		this.userService.deleteById(3);
		render("success.jsp");
	}

}
